#include "eeconfig.h"
#include "EEPROM.h"
#include "ui.h"
#include "disp.h"
#include "stdint.h"
#include "wifi.h"
#include "weather.h"

uint8_t isVaildChar(char c)
{
    if(c<33 || c>126)
        return 0;
    return 1;
}

uint16_t eepromWriteStr(char *s, uint8_t len, uint16_t addr)
{
    uint8_t i = 0;

    for(i=0; i<len; i++, addr++)
    {
        EEPROM.write(addr, s[i]);
    }
    EEPROM.write(addr, '\0');
    EEPROM.commit();
    return (addr+1);
}

uint16_t eepromReadStr(char *s, uint16_t addr)
{
    uint8_t len = 0;

    while(len < 20)
    {
        s[len] = EEPROM.read(addr);
        if(s[len]<33 || s[len]>126)
        {
            s[len] = '\0';
            break;
        }
        len++;
        addr++;
    }
    
    return (addr+1);
}

void saveColorConfig(uint32_t color, uint8_t brightness)
{
    EEPROM.begin(4000);
    EEPROM.write(3000, (color>>16)&0xFF);
    EEPROM.write(3001, (color>>8)&0xFF);
    EEPROM.write(3002, color&0xFF);
    EEPROM.write(3003, brightness);
    EEPROM.end();
}

void readColorConfig(void)
{
    uint8_t r, g, b, bright;
    EEPROM.begin(4000);
    r = EEPROM.read(3000);
    g = EEPROM.read(3001);
    b = EEPROM.read(3002);
    if(r<=255 || g<=255 || b<= 255 )
        setFontColor((uint32_t)r<<16 | (uint32_t)g<<8 | (uint32_t)b);
    else
        setFontColor(0x0000FF);

    bright = EEPROM.read(3003);
    if(bright <= 100)
        setConfigBrightness(bright);
    else
        setConfigBrightness(50);
    EEPROM.end();
}

/*save config to EEPROM*/
void saveConfig(void)
{
    uint8_t len = 0, i = 0;
    uint16_t addr = 3100;

    Serial.println("save config...");
    EEPROM.begin(4000);

    strcpy(wifi_ssid, wm.getWiFiSSID().c_str());
    len = strlen(wifi_ssid);
    addr = eepromWriteStr(wifi_ssid, len, addr);
    Serial.printf("ssid:%s stored, len:%d\r\n", wifi_ssid, len);

    strcpy(wifi_pwd, wm.getWiFiPass().c_str());
    len = strlen(wifi_pwd);
    addr = eepromWriteStr(wifi_pwd, len, addr);
    Serial.printf("password:%s stored, len:%d\r\n", wifi_pwd, len);

    strcpy(blinker_key, set_blinker_key.getValue());
    len = strlen(blinker_key);
    addr = eepromWriteStr(blinker_key, len ,addr);
    Serial.printf("blinker key:%s stored, len:%d\r\n", blinker_key, len);

    strcpy(weather_key, set_weather_key.getValue());
    len = strlen(weather_key);
    addr = eepromWriteStr(weather_key, len ,addr);
    Serial.printf("weather_key:%s stored, len:%d\r\n", weather_key, len);

    strcpy(weather_city, set_city.getValue());
    len = strlen(weather_city);
    addr = eepromWriteStr(weather_city, len ,addr);
    Serial.printf("city:%s stored, len:%d\r\n", weather_city, len);

    EEPROM.end();

    force_request = 1;
}

/*read config from EEPROM*/
void readConfig(void)
{
    uint8_t str_err = 0;
    uint8_t len = 0, i = 0;
    uint16_t addr = 3100;
    char setting[20];

    EEPROM.begin(4000);
    Serial.println("read config...");

    memset(setting, 0, 20);
    addr = eepromReadStr(setting, addr);
    strcpy(wifi_ssid, setting);
    Serial.printf("ssid:%s\r\n", wifi_ssid);

    memset(setting, 0, 20);
    addr = eepromReadStr(setting, addr);
    strcpy(wifi_pwd, setting);
    Serial.printf("pwd:%s\r\n", wifi_pwd);

    memset(setting, 0, 20);
    addr = eepromReadStr(setting, addr);
    strcpy(blinker_key, setting);
    Serial.printf("blinker key:%s\r\n", blinker_key);

    memset(setting, 0, 20);
    addr = eepromReadStr(setting, addr);
    strcpy(weather_key, setting);
    Serial.printf("weather key:%s\r\n", weather_key);

    memset(setting, 0, 20);
    addr = eepromReadStr(setting, addr);
    strcpy(weather_city, setting);
    Serial.printf("city:%s\r\n", weather_city);

    EEPROM.end();


}
